<?php

namespace app\api\Service;

use think\Db;
use think\Config;
use app\common\library\Auth;

class ServicePackageService
{
    // 获取列表
    public static function getServicePackageList($data)
    {
        $limit = isset($data['limit']) ? $data['limit'] : 10;
        $list = \app\admin\model\ServicePackage::where(['status'=>'normal'])->order('weigh desc')->field('id,title,image')->paginate($limit)->toArray();

        return ['code'=>1,'msg'=>'查询成功','data'=>['total'=>$list['total'],'list'=>$list['data']]];
    }

    // 获取详情
    public static function getServicePackageDetails($data)
    {
        $auth = Auth::instance();
        $id = $data['id'];
        $user_id = $auth->id;
        if(!$id) return ['code'=>0,'msg'=>'无效参数','data'=>''];
        $details = \app\admin\model\ServicePackage::where(['id'=>$id,'status'=>'normal'])->field('id,title,image,description,sales,ficti')
            ->find();
        if(!$details) return ['code'=>0,'msg'=>'数据不存在','data'=>''];
        $details['sales'] = bcadd($details['sales'], $details['ficti']);
        unset($details['ficti']);

        // 记录每天用户第一次点击
        if($user_id){
            // 1.查询用户当天是否有点击记录
            $count = \app\admin\model\ServiceClickLog::where(['user_id'=>$user_id,'service_id'=>$id])->whereTime('createtime',
                'today')->count();
            // 2. 如果没有点击记录，插入一条记录
            if(!$count){
                \app\admin\model\ServiceClickLog::create(['user_id'=>$user_id, 'service_id'=>$id]);
            }
        }

        return ['code'=>1,'msg'=>'查询成功','data'=>$details];
    }

    // 获取接送服务价格
    public static function getTransferPrice($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;
        $transfer_service_price = Config::get('site.transfer_service_price');
        return ['code' => 1, 'msg' => '查询成功', 'data' => $transfer_service_price];
    }

    // 获取接送服务价格
    public static function getLifePrice($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;
        $transfer_service_price = Config::get('site.life_service_price');
        return ['code' => 1, 'msg' => '查询成功', 'data' => $transfer_service_price];
    }

    // 获取定制服务列表
    public static function getCustomServiceList($data){
        $auth = Auth::instance();
        $user_id = $auth->id;
        $list = \app\admin\model\Service::where(['status'=>'normal'])->order('weigh desc')->field('id,title,price,duration')->select();
        return ['code' => 1, 'msg' => '查询成功', 'data' => $list];
    }
}